package com.school.controller;

import java.net.UnknownHostException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.school.entity.HljtClass;
import com.school.entity.HljtExam;
import com.school.entity.HljtSchoolnotify;
import com.school.entity.android.Notice;
import com.school.entity.android.Pattern;
import com.school.service.impl.HljtClassServiceIpml;
import com.school.service.impl.HljtExamServiceImpl;
import com.school.service.impl.HljtSchoolNotifyServiceImpl;
import com.school.util.JsonInterface;

import net.sf.json.JSONObject;


@Controller
public class HljtPatternController {

	@Autowired
	private HljtClassServiceIpml hljtClassServiceIpml;
	@Autowired
	private HljtExamServiceImpl hljtExamServiceImpl;
	@Autowired
	private HljtSchoolNotifyServiceImpl hljtSchoolNotifyServiceImpl;
	/**
	 * 考试模式
	 * @param classid
	 * @return
	 * @throws UnknownHostException 
	 */
	@RequestMapping("/Examination")
	@ResponseBody
	public JsonInterface toBeOnDuty(Integer classid){
		JsonInterface jsonInterface=new JsonInterface();
		if(classid !=null) {
			
			HljtClass hljtClass=hljtClassServiceIpml.findClass(classid);
			
			HljtExam exam=hljtExamServiceImpl.fiandExam(classid);
			
			if(null!=hljtClass && null!=exam) {
				Pattern pattern=new Pattern();
				//获取班级名字
				String className=hljtClass.getGrade()+"("+hljtClass.getClasses()+")";
				
				pattern.setClassName(className);
				pattern.setTestName(exam.getTestname());
				pattern.setSubject(exam.getSubject());
				pattern.setEndTime(exam.getEndtime());
				pattern.setStartTime(exam.getStarttime());
				pattern.setTeacherName(exam.getTeachername());
				pattern.setContent(exam.getContent());
				
				JSONObject json=JSONObject.fromObject(pattern);
				jsonInterface.setData(json.toString());
				jsonInterface.setErrCode(0);
				jsonInterface.setErrMsg("请求成功");
			}else {
				jsonInterface.setErrCode(1);
				jsonInterface.setErrMsg("无数据，请添加数据！");
			}
		}else {
			jsonInterface.setErrCode(3);
			jsonInterface.setErrMsg("请求失败");
		}
		
		return jsonInterface;
	}
	
	/**
	 * 全屏文字通知（紧急通知）
	 * @param classid
	 * @return
	 * @throws UnknownHostException 
	 */
	@RequestMapping("/FullScreen")
	@ResponseBody
	public JsonInterface toBeOnDuty(){
		JsonInterface jsonInterface=new JsonInterface();
		List<HljtSchoolnotify> Schoolnotify=hljtSchoolNotifyServiceImpl.findSchoolnotify("6");
			if(null!=Schoolnotify&&Schoolnotify.size()>0) {
				
					Notice notice=new Notice();
					notice.setNoticeName(Schoolnotify.get(Schoolnotify.size()-1).getTitle());
					notice.setNoticeContent(Schoolnotify.get(Schoolnotify.size()-1).getContent());
					notice.setVoice(Schoolnotify.get(Schoolnotify.size()-1).getVoice());
					notice.setFullScreen(Schoolnotify.get(Schoolnotify.size()-1).getFullscreen());
					
				JSONObject json=JSONObject.fromObject(notice);
				jsonInterface.setData(json.toString());
				jsonInterface.setErrCode(0);
				jsonInterface.setErrMsg("请求成功");
				
			}else {
				jsonInterface.setErrCode(1);
				jsonInterface.setErrMsg("无数据，请添加数据！");
			}
		return jsonInterface;
	}

}
